app.controller('userController',function ($scope, $http) {
    //初始化
    $scope.initMethod = function () {
        $scope.User = {};
    }
    $scope.data = localStorage.getItem("token");

    $scope.code = "";

    /*$scope.User.mobile = "";

    $scope.User.password = "";*/

    $http.defaults.headers.common = {'tou':$scope.data};

    //登录
    $scope.login=function () {
        $http.post("http://localhost:8082/user/login",$scope.User).success(function (result) {
            alert(result.msg);
        if (result.code==20000){
            $scope.User={};
            localStorage.setItem("token",result.data);
            alert(localStorage.getItem("token"));
            location.href ="anth_page.html";
            //$scope.tofind();
        }

    })
    }

    //测试
    $scope.tofind = function(){
        $http.get("http://localhost:8082/user/findAll").success(function (result) {
            alert("sdfghyj");
    })
    }



    //获取验证码
    $scope.codeSms = function (mobile) {
        if ($scope.User.mobile==null) {
            alert("请输入手机号");
        }else {
            $http.post("http://localhost:8082/user/codeSms?mobile="+$scope.User.mobile).success(function (result){

                if (result.code==20000){
                alert(result.data);
                 }
                if (result.code==20001) {
                    alert("请稍后获取");
                }

        })
        }

    }
    //手机验证码 登录
    $scope.codeLogin = function () {
        $http.post("http://localhost:8082/user/codeLogin?code="+$scope.code,$scope.User).success(function (result) {
            if (result.code == 20000) {
            localStorage.setItem("token", result.data);
            alert(localStorage.getItem("token"));
            alert(result.msg);
            location.href = "anth_page.html";
        } else {
            alert(result.msg);
        }
    })
    }
    //忘记密码
    $scope.forget = function () {
        $http.post("http://localhost:8082/user/forget?code="+$scope.code,$scope.User).success(function (result){
            if(result.code == 20000){
            alert(result.msg);
            location.href = "password_login.html";
        }else {
            alert(result.msg);
        }
    })
    }
    //判断密码
    $scope.confirmMM = function (password) {
        var oldpwd = $scope.oldpwd;
        if (oldpwd == password) {
            $scope.newPwdSpan = "√";
        }else{
            $scope.newPwdSpan = "×";
        }
    }
    //密码正则
    /*$scope.judgeCount = function () {
        var oldpwd = $scope.oldpwd;
        if (/^[a-zA-Z\w{5,17}]$/.test(oldpwd)) {
            $scope.oldPwdSpan = "√";
        }else{
            $scope.oldPwdSpan = "×";
        }
    }*/
    //注册
    $scope.register = function () {
        $http.post("http://localhost:8082/user/register?code="+$scope.code,$scope.User).success(function (result){
            if (result.code == 20000){
            alert(result.msg);
            location.href = "password_login.html";
        }else {
            alert(result.msg);
        }
    })
    }
})
